package com.yl.plugin.method;

import com.yl.plugin.method.base.BaseMethod;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;

import java.util.List;

/**
 * 分页查询
 * @author liuxubo
 */
public class PageMethod implements BaseMethod {
    @Override
    public int order() {
        return 0;
    }

    @Override
    public Method buildMethod(IntrospectedTable introspectedTable) {
        // 实体类类型
        FullyQualifiedJavaType modelType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());

        // 访问类型：public
        JavaVisibility methodVisibility = JavaVisibility.PUBLIC;

        // 返回类型：PageInfo<ApproveLog>
        FullyQualifiedJavaType returnType = new FullyQualifiedJavaType("PageInfo");
        // 设置泛型参数
        returnType.addTypeArgument(modelType);

        // 设置参数类型是对象
        FullyQualifiedJavaType parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
        Parameter methodParameter0 = new Parameter(parameterType, StringUtils.uncapitalize(parameterType.getShortName()));

        FullyQualifiedJavaType parameterType1 = new FullyQualifiedJavaType("Integer");
        Parameter methodParameter1 = new Parameter(parameterType1, "pageNum");
        FullyQualifiedJavaType parameterType2 = new FullyQualifiedJavaType("Integer");
        Parameter methodParameter2 = new Parameter(parameterType2, "pageSize");


        // 创建方法对象
        Method method = new Method("page");
        // 设置访问类型
        method.setVisibility(methodVisibility);
        // 设置返回类型对象
        method.setReturnType(returnType);
        // 设置方法参数
        method.addParameter(methodParameter0);
        method.addParameter(methodParameter1);
        method.addParameter(methodParameter2);

        List<Parameter> parameters = method.getParameters();

        // 设置备注
        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * 分页查询");
        method.addJavaDocLine(" * ");
        parameters.forEach(parameter -> method.addJavaDocLine(" * @param " + parameter.getName()));
        method.addJavaDocLine(" */");

        return method;
    }

}
